<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="分销名称" prop="name">
        <el-input v-model="queryParams.name" placeholder="请输入分销名称" clearable size="small"
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="分销层级" prop="level">
        <el-select v-model="queryParams.level" placeholder="请选择分销层级" clearable @keyup.enter.native="handleQuery"
          size="small">
          <el-option v-for="dict in levelSelect" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['bizsys:ratio:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['bizsys:ratio:remove']">删除</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="ratioList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="#" align="center" prop="id" width="60"/>
      <el-table-column label="分销名称" align="center" prop="name" />
      <el-table-column label="分销层级" align="center" prop="level" :formatter="levelFormatter" />
      <el-table-column label="一级分销" align="center" prop="firstRatio" :formatter="rateFormatter" />
      <el-table-column label="二级分销" align="center" prop="secondRatio" :formatter="rateFormatter" />
      <el-table-column label="三级分销" align="center" prop="thirdRatio" :formatter="rateFormatter" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['bizsys:ratio:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['bizsys:ratio:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改奖励分销比对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="分销名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入分销名称" />
        </el-form-item>
        <el-form-item label="分销层级" prop="level">
          <el-select @change="levelChange" v-model="form.level" placeholder="请选择分销层级" clearable size="small">
            <el-option v-for="dict in levelSelect" :key="dict.value" :label="dict.label" :value="dict.value" />
          </el-select>
        </el-form-item>
        <el-form-item label="一级分销" prop="firstRatio" v-show="oneShow">
          <el-input style="width: 15%;" oninput="value=value.replace(/[^\d]/g,'')" maxLength='3' v-model="form.firstRatio" />%
        </el-form-item>
        <el-form-item label="二级分销" prop="secondRatio" v-show="twoShow">
          <el-input style="width: 15%;" oninput="value=value.replace(/[^\d]/g,'')" maxLength='3' v-model="form.secondRatio" />%
        </el-form-item>
        <el-form-item label="三级分销" prop="thirdRatio" v-show="threeShow">
          <el-input style="width: 15%;" oninput="value=value.replace(/[^\d]/g,'')" maxLength='3' v-model="form.thirdRatio"  />%
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import {
    listRatio,
    getRatio,
    delRatio,
    addRatio,
    updateRatio,
    exportRatio
  } from "@/api/bizsys/award_distribution_ratio";

  export default {
    name: "Ratio",
    components: {},
    data() {
      return {
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 奖励分销比表格数据
        ratioList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          name: null,
          level: null,
          firstRatio: null,
          secondRatio: null,
          thirdRatio: null,
          state: null,
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {
          name: [{
            required: true,
            message: "分销名称不能为空",
            trigger: "blur"
          }],
          level: [{
            required: true,
            message: "分销层级不能为空",
            trigger: "blur"
          }],
          firstRatio: [{
            required: true,
            message: "一级分销不能为空",
            trigger: "blur"
          }],
          secondRatio: [{
            required: true,
            message: "二级分销不能为空",
            trigger: "blur"
          }],
          thirdRatio: [{
            required: true,
            message: "三级分销不能为空",
            trigger: "blur"
          }],
        },
        levelSelect: [{
          value: 1,
          label: '一级分销',
        }, {
          value: 2,
          label: '二级分销',
        }, {
          value: 3,
          label: '三级分销',
        }],
        oneShow: false,
        twoShow: false,
        threeShow: false,
      };
    },
    created() {
      this.getList();
    },
    methods: {
      levelFormatter(row, column) {
        return this.levelSelect.filter(obj => obj.value === parseInt(row.level))[0].label;
      },
      rateFormatter(row, column) {
        if (column.property == 'firstRatio') {
          return row.firstRatio==null?'':row.firstRatio+'%'
        }
        if (column.property == 'secondRatio') {
          return row.secondRatio==null?'':row.secondRatio+'%'
        }
        if (column.property == 'thirdRatio') {
          return row.thirdRatio==null?'':row.thirdRatio+'%'
        }
      },
      levelChange(value) {
        console.info("select:" + value);
        if (value === 1) {
          this.oneShow = true;
          this.twoShow = false;
          this.threeShow = false;
          this.rules.firstRatio[0].required = true;
          this.rules.secondRatio[0].required = false;
          this.rules.thirdRatio[0].required = false;
          return;
        }
        if (value === 2) {
          this.oneShow = true;
          this.twoShow = true;
          this.threeShow = false;
          this.rules.firstRatio[0].required = true;
          this.rules.secondRatio[0].required = true;
          this.rules.thirdRatio[0].required = false;
          return;
        }
        if (value === 3) {
          this.oneShow = true;
          this.twoShow = true;
          this.threeShow = true;
          this.rules.firstRatio[0].required = true;
          this.rules.secondRatio[0].required = true;
          this.rules.thirdRatio[0].required = true;
          return;
        }
      },
      /** 查询奖励分销比列表 */
      getList() {
        this.loading = true;
        listRatio(this.queryParams).then(response => {
          this.ratioList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          id: null,
          name: null,
          level: null,
          firstRatio: null,
          secondRatio: null,
          thirdRatio: null,
          state: null,
          remark: null,
          createBy: null,
          createTime: null,
          updateBy: null,
          updateTime: null
        };
        this.oneShow = false;
        this.twoShow = false;
        this.threeShow = false;
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id)
        this.single = selection.length !== 1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true;
        this.title = "添加奖励分销比";
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        let _this = this;
        this.reset();
        const id = row.id || this.ids
        getRatio(id).then(response => {
          this.form = response.data;
          _this.levelChange(this.form.level);
          this.open = true;
          this.title = "修改奖励分销比";
        });
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.id != null) {
              updateRatio(this.form).then(response => {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              });
            } else {
              addRatio(this.form).then(response => {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const ids = row.id || this.ids;
        this.$confirm('是否确认删除奖励分销比编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delRatio(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
      },
      /** 导出按钮操作 */
      handleExport() {
        const queryParams = this.queryParams;
        this.$confirm('是否确认导出所有奖励分销比数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportRatio(queryParams);
        }).then(response => {
          this.download(response.msg);
        })
      }
    }
  };
</script>
